package org.darkforest.admin.controller;

import org.darkforest.common.services.BoothGoodsService;
import org.darkforest.common.services.SettingService;
import org.darkforest.common.util.CsvUtil;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.Utils;
import org.darkforest.dao.mapper.*;
import org.darkforest.enums.SettingNameEnum;
import org.darkforest.model.*;
import org.darkforest.ucenter.auth.AuthPassport;
import org.darkforest.ucenter.service.UserOrderService;
import org.darkforest.vo.AjaxReturnData;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by valentina on 15/5/11.
 * 运营管理控制类
 */
@Controller
@RequestMapping("operation")
public class OperationController {

    @Autowired
    private BoothGoodsService boothGoodsService;

    @Autowired
    private PMiaoshaMapper pMiaoshaMapper;

    @Autowired
    private PXianshiMapper pXianshiMapper;

    @Autowired
    private PXianshiGoodsMapper pXianshiGoodsMapper;

    @Autowired
    private PXianshiQuotaMapper pXianshiQuotaMapper;

    @Autowired
    private OrderBillMapper orderBillMapper;

    @Autowired
    private OrderStatisMapper orderStatisMapper;

    @Autowired
    private StoreMapper storeMapper;



    @RequestMapping("recommendList")
    @AuthPassport
    public ModelAndView recommendList(@RequestParam(required = false, defaultValue = "0") String recommendType,
                                      @RequestParam(required = false, defaultValue = "1") String start
                                      ) {
        ModelAndView view = new ModelAndView("operation/recommend/recommendList");
        PBoothGoods pBoothGoods  = new PBoothGoods();
        pBoothGoods.setStart(Integer.parseInt(start));
        if(!"0".equals(recommendType)){
            pBoothGoods.setRecommondType(Integer.parseInt(recommendType));
        }
        pBoothGoods = boothGoodsService.selectRecommondGoodsByPage(pBoothGoods);
        view.addObject("page", pBoothGoods);
        return view;
    }

    /**
     * 删除推荐展位
     *
     * @param gid
     * @return
     */
    @RequestMapping("delRecommondGoods")
    public
    @ResponseBody
    AjaxReturnData delRecommondGoods(Integer gid) {
        AjaxReturnData ajaxReturnData = this.boothGoodsService.delRecommondGoods(gid);
        return ajaxReturnData;
    }


    /**
     * 管理员审核推荐商品
     * @param recommendId
     * @param verifyType 1 通过 2 不通过
     * @return
     */
    @RequestMapping(value = "verifyRecommendGoods")
    public @ResponseBody AjaxReturnData verifyRecommendGoods(Integer recommendId,Integer verifyType,String reason) throws Exception {

        AjaxReturnData ajaxReturnData =   boothGoodsService.verifyRecommendGoods(recommendId,verifyType,reason);
        return ajaxReturnData;
    }

    @RequestMapping("consultList")
    @AuthPassport
    public ModelAndView consultList() {
        ModelAndView view = new ModelAndView("operation/consult/consultList");
        return view;
    }

    @RequestMapping("billList")
    @AuthPassport
    public ModelAndView billList(OrderStatis orderStatis) {
        orderStatis.setResultList(orderStatisMapper.selectByCondition(orderStatis));
        ModelAndView view = new ModelAndView("operation/bill/billList");
        view.addObject("page", orderStatis);
        return view;
    }

    @RequestMapping("billList/statisList")
    @AuthPassport
    public ModelAndView billList(OrderBill orderBill) {
        ModelAndView view = new ModelAndView("operation/bill/statisList");
        List<OrderBill> list = orderBillMapper.selectByCondition(orderBill);
        List<Store> stores = storeMapper.selectAllStore();
        orderBill.setResultList(list);
        view.addObject("page", orderBill);
        view.addObject("storeList", stores);

        return view;
    }
    @RequestMapping("billList/download")
    @AuthPassport
    public ModelAndView download(HttpServletRequest request, HttpServletResponse response) throws Exception{
        //查询条件
        String obStoreId = request.getParameter("obStoreId");
        String obState = request.getParameter("obState");
        OrderBill orderBill = new OrderBill();
        if (obStoreId != null && !"".equals(obStoreId)) {
            orderBill.setObStoreId(Integer.valueOf(obStoreId));
        }
        orderBill.setObState(obState);
        orderBill.setPageSwitch(false);
        List<OrderBill> list = orderBillMapper.selectByCondition(orderBill);
        List exportData = new ArrayList<Map>();
        for(OrderBill ob:list){
            Map row1 = new LinkedHashMap<String, String>();
            row1.put("1", ob.getObNo().toString());
            row1.put("2", ob.getObStartDateStr());
            row1.put("3", ob.getObEndDateStr());
            row1.put("4", ob.getObOrderTotals().toString());
            row1.put("5", ob.getObShippingTotals().toString());
            row1.put("6", ob.getObCommisTotals().toString());
            row1.put("7", ob.getObOrderReturnTotals().toString());
            row1.put("8", ob.getObCommisReturnTotals().toString());
            row1.put("9", ob.getObStoreCostTotals().toString());
            row1.put("10", ob.getObResultTotals().toString());
            row1.put("11", ob.getOsMonth()+ob.getObCreateDateStr());
            if ("1".equals(ob.getObState())) {
                row1.put("12", "已出账");
            } else if ("2".equals(ob.getObState())) {
                row1.put("12", "商家已确认");
            } else if ("3".equals(ob.getObState())) {
                row1.put("12", "平台已审核");
            } else if ("4".equals(ob.getObState())) {
                row1.put("12", "结算完成");
            }
            row1.put("13", ob.getObStoreId().toString());
            row1.put("14", ob.getObStoreName());
            exportData.add(row1);
        }
        LinkedHashMap map = new LinkedHashMap();
        map.put("1", "账单编号");
        map.put("2", "开始日期");
        map.put("3", "结束日期");
        map.put("4", "订单金额");
        map.put("5", "运费");
        map.put("6", "佣金金额");
        map.put("7", "退款金额");
        map.put("8", "退还佣金");
        map.put("9", "店铺费用");
        map.put("10", "本期应结");
        map.put("11", "出账日期");
        map.put("12", "账单状态");
        map.put("13", "店铺ID");
        map.put("14", "店铺名称");

        String ctxPath = request.getSession().getServletContext().getRealPath("/")+ "temp\\";
//        String fileName = "商家账单列表";
        String fileName = "merchant_billing_list";
        CsvUtil.createCSVFile(exportData, map, ctxPath, fileName);
        CsvUtil.downloadCSVFile(fileName+".csv",ctxPath,request,response);
        return null;
    }

    //限时折扣
    @RequestMapping(value = "xianshiList")
    @AuthPassport
    public ModelAndView xianshiList(PXianshi pXianshi) {
//        pXianshi.setActiveState(null);
        pXianshi.setResultList(pXianshiMapper.findPXianShiByCondition(pXianshi));
        ModelAndView view = new ModelAndView("operation/xianshi/xianshiList");
        view.addObject("page", pXianshi);
        return view;
    }

    //限时折扣
    @RequestMapping(value = "xianshiList/xianshiDetail")
    public ModelAndView xianshiList(Integer xianshiId) {
        ModelAndView view = new ModelAndView("operation/xianshi/xianshiDetail");
        view.addObject("info", pXianshiMapper.selectByPrimaryKey(xianshiId));
        PXianshiGoods pXianshiGoods = new PXianshiGoods();
        pXianshiGoods.setXianshiId(xianshiId);
        pXianshiGoods.setResultList(pXianshiGoodsMapper.findPXianshiGoodsListByCondition(pXianshiGoods));
        view.addObject("page", pXianshiGoods);
        return view;
    }

    //限时折扣
    @RequestMapping(value = "xianshiquota")
    public ModelAndView xianshiquota(PXianshiQuota pXianshiQuota) {
        ModelAndView view = new ModelAndView("operation/xianshi/xianshiquota");
        pXianshiQuota.setResultList(pXianshiQuotaMapper.selectByCondition(pXianshiQuota));
        view.addObject("page", pXianshiQuota);
        return view;
    }

    //取消状态
    @RequestMapping(value = "xianshiUpdateState")
    public ModelAndView xianshiUpdateState(PXianshi pXianshi) throws Exception {
        pXianshiMapper.updateByPrimaryKeySelective(pXianshi);
        PXianshiGoods pXianshiGoods = new PXianshiGoods();
        pXianshiGoods.setXianshiId(pXianshi.getXianshiId());
        pXianshiGoods.setState(pXianshi.getState());
        pXianshiGoodsMapper.updatePXianshiGoodsState(pXianshiGoods);
        return new ModelAndView("redirect:/operation/xianshiList");
    }

    @RequestMapping(value = "xianshiRecommend")
    public ModelAndView xianshiRecommend(PXianshiGoods pXianshiGoods) throws Exception {
        pXianshiGoodsMapper.updateByPrimaryKeySelective(pXianshiGoods);
        return new ModelAndView("redirect:/operation/xianshiDetail?xianshiId=" + pXianshiGoods.getXianshiId());
    }

    @RequestMapping(value = "xianshiDelete")
    public ModelAndView xianshiDelete(Integer xianshiId) throws Exception {
        if (xianshiId != null) {
            pXianshiMapper.deleteByPrimaryKey(Integer.valueOf(xianshiId));
        }
        return new ModelAndView("redirect:/operation/xianshiList");
    }


    //秒杀列表

    @RequestMapping("miaoshaList")
    @AuthPassport
    public ModelAndView miaoshaList(PMiaosha pMiaosha) {
        pMiaosha.setResultList(pMiaoshaMapper.findPMiaoShaByCondition(pMiaosha));
        ModelAndView view = new ModelAndView("operation/miaosha/miaoshaList");
        view.addObject("page", pMiaosha);
        return view;
    }

    @RequestMapping("sellerbillList")
    @AuthPassport
    public ModelAndView sellerbillList() {
        ModelAndView view = new ModelAndView("operation/bill/sellerbillList");
        return view;
    }
}
